package vn.student;

import vn.student.controller.ViewEditSemesterDetailEditorController;
import vn.student.mylib.DateDateDialog;
import vn.student.mylib.TextInputDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;

	/*
	
		Class name: ViewEditSemesterDetailEditor.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity dung de xem thong tin chi tiet mot hoc ki va chinh sua thong tin.
		
	*/

public class ViewEditSemesterDetailEditor extends Activity{
	String id = null;
	Activity act = this;
	ViewEditSemesterDetailEditorController controller = null;
	boolean isCreateNew = false;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_semester_detail);
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickBackButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickSaveButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.editNameBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editSemesterName();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.editDurationBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editDuration();
			}
		});
		Intent intent = this.getIntent();
		id = intent.getExtras().getString("id");
		isCreateNew = intent.getExtras().getBoolean("isCreateNew");
		controller = new ViewEditSemesterDetailEditorController(this,id,isCreateNew);
		controller.showCourseDetail(id);
	}
	private void onClickBackButton(){
		this.setResult(Activity.RESULT_CANCELED);
		this.finish();
	}
	private void onClickSaveButton(){
		if(controller.updateSemesterInfo()){
			this.setResult(Activity.RESULT_OK);
			this.finish();
		}
		else{
			//Create a builder 
		      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
		      builder.setTitle("Alert Window"); 
		      //Create the dialog 
		      AlertDialog ad = builder.create(); 
		       
		      //show 
		      ad.show(); 
		}
			
	}
	private void editDuration(){
		String[] dateStart = controller.semesterInfo.dateStart.split("-");
		if(dateStart.length!=3)
			return;
		String[] dateEnd = controller.semesterInfo.dateEnd.split("-");
		if(dateEnd.length!=3)
			return;
		DateDateDialog.viewDialog("Course duration",dateStart,dateEnd, act, new DateDateDialogOnOkClickListener());
	}
	private void editSemesterName(){
		TextInputDialog.viewDialog("Input semester name",controller.semesterInfo.semesterName, act, new InputTextDialogOnOkClickListener());
	}
	class DateDateDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String tmp;
			DatePicker date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateStartPicker);
			String dateStartStr = "";
			dateStartStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp;
			
			date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateEndPicker);
			
			String dateEndStr = "";
			dateEndStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp;
			
			controller.editDuration(dateStartStr,dateEndStr);
			
			controller.showSemesterDetail();
		}
	}
	class InputTextDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String name = ((EditText) ((Dialog) dialog).findViewById(R.id.editTextView)).getText().toString();
			controller.editCourseName(name);

			controller.showSemesterDetail();
		}
	}
}
